﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Text.RegularExpressions;

namespace GiveAQuiz.Common.Converters
{
  /// <summary>
  /// Converter used to convert plain text to RichText XAML for the RichTextBox and add
  /// hyperlinks for any links included.
  /// </summary>
  public class RichTextConverter : IValueConverter
  {

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      if (targetType == typeof(string))
      {
        var text = (string) value;
        Regex ex = new Regex(@"(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?");
        ex.Replace(text, m => 
          {
            return string.Format("<Hyperlink NavigateUri=\"{0}\" TargetName=\"_blank\">{0}</Hyperlink>", m.Value);
          });
        return string.Concat("<Paragraph>", text, "</Paragraph>");
      }

      return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      // Not implemented
      throw new NotImplementedException();
    }
  }
}
